package com.platform.modules.wallet.controller;


import cn.hutool.core.util.StrUtil;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.utils.SnowflakeUtils;
import com.platform.common.version.ApiVersion;
import com.platform.common.version.VersionEnum;
import com.platform.common.web.controller.BaseController;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.wallet.domain.Wallet;
import com.platform.modules.wallet.service.WalletKPayService;
import com.platform.modules.wallet.service.WalletService;
import com.platform.modules.wallet.vo.RechargeCashVo01;
import com.platform.modules.wallet.vo.WalletVo04;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 钱包
 */
@RestController
@Slf4j
@RequestMapping("/wallet")
public class WalletKPayController extends BaseController {


    @Resource
    private WalletService walletService;
    @Resource
    private ChatUserService chatUserService;
    @Resource
    private WalletKPayService walletKPayService;

    /**
     * 获取钱包地址
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getWalletCode")
    public AjaxResult getWalletCode() {

        Long userId = ShiroUtils.getUserId();
        ChatUser chatUser = chatUserService.getById(userId);
        Wallet wallet = walletService.getWallet(userId);

        if (StrUtil.isBlank(wallet.getWalletAddress())){
            String walletcode = walletKPayService.register(chatUser.getPhone(), wallet.getName(), chatUser.getNickName());
            wallet.setWalletAddress(walletcode);
            walletService.update(wallet);
        }

        return AjaxResult.success(wallet.getWalletAddress());
    }

    /**
     * 找回钱包地址
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/rebackWalletCode")
    public AjaxResult rebackWalletCode(@Validated @RequestBody WalletVo04 vo) {

        Long userId = ShiroUtils.getUserId();
        ChatUser chatUser = chatUserService.getById(userId);
        String walletCode = walletKPayService.getWalletCode(chatUser.getPhone(), vo.getPassword());
        Wallet wallet = walletService.getWallet(userId);
        wallet.setWalletAddress(walletCode);
        walletService.update(wallet);

        return AjaxResult.success(wallet.getWalletAddress());
    }

    /**
     * 获取钱包余额
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getWalletBalance")
    public AjaxResult getWalletBalance() {
        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);
        return AjaxResult.success(walletKPayService.getBalance(wallet.getWalletAddress()));
    }



    /**
     * 存款 钱包 > 平台
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/testDeposit")
    public AjaxResult deposit() {
        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);

        walletKPayService.deposit(
                wallet.getWalletAddress(),
                new BigDecimal("1"),
                String.valueOf(SnowflakeUtils.getNextId()),
                "123456");

        return AjaxResult.success("存款成功");
    }

    /**
     * 取款 平台 > 钱包
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/testWithdraw")
    public AjaxResult withdraw() {
        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);

        walletKPayService.withdraw(
                wallet.getWalletAddress(),
                new BigDecimal("10"),
                String.valueOf(SnowflakeUtils.getNextId()),
                "123456");

        return AjaxResult.success("取款成功");
    }
}
